﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

using GPTAS.Core;
namespace GPTAS.Infrastructure
{
    public class Allocation
    {
        public Allocation()
        {
        }

        public Allocation(string name)
        {
            Name = name;
        }

        public Allocation(string name,int id)
        {
            Name = name;
            Id = id;
        }

        public int Id
        {
            get;
            set;
        }
        public string Name
        {
            get;
            set;
        }

        public Dictionary<int,Group> Groups
        {
            get;
            set;
        }

        public Dictionary<int,Item> Items
        {
            get;
            set;
        }

        private Dictionary<int,Constraint> Constraints
        {
            get;
            set;
        }

        public string Status
        {
            get;
            set;
        }

        public void GetGroups()
        {
            DataTable dt = Database.Database.Instance.GetGroups(Id);
            if (dt != null)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    Group g = new Group(Convert.ToInt32(dt.Rows[i].ItemArray[0]), dt.Rows[i].ItemArray[1].ToString());                    
                    Groups.Add(g.Id,g);
                }
            }
        }

        public void GetItems()
        {
            DataTable dt = Database.Database.Instance.GetItems(Id);
            if (dt != null)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    int itemid=Convert.ToInt32(dt.Rows[i].ItemArray[0]);
                    string itemname=dt.Rows[i].ItemArray[1].ToString();
                    Item it=new Item(itemid,itemname);
                    Items.Add(itemid,it);
                }
            }
        }

        public void GetItemsForGroups()
        {
            foreach (var item in Items)
            {    
                int groupid=0;
                if (Allocator.Instance.IteminGroup.TryGetValue(item.Key, out groupid))
                {
                    Group g = new Group();
                    if (Groups.TryGetValue(groupid, out g))
                    {
                        g.Items.Add(item.Key,item.Value);
                    }
                }
            }
        }
    }
}
